package principal;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;

import cromosomas.Tablero;

public class TableroGUI extends javax.swing.JPanel {
    //private ImageIcon comida, hormiga;
    private JPanel [][] casillas ;
    private Tablero tablero;
        
    public TableroGUI() {
        initComponents();

    }

    public TableroGUI(Tablero tab, int size) {
        //initComponents();
    	tablero=tab;
        int x,y;
        setLayout(new java.awt.GridLayout(size, size));
//        cargarImagenes();
        casillas = new JPanel[size][size];
        for (int i = 0; i < size; i++){
            for (int j = 0; j < size; j++){
                casillas[j][i] = new JPanel(); 
                inicializarCasilla(casillas,j,i);
                x = (i * 25)+1;
                y = (j * 25)+1;
                casillas[j][i].setBounds(x, y, 25, 25);
                this.add(casillas[j][i]);
            }
        }
    }
       
    private void inicializarCasilla(JPanel[][] cas, int i, int j) {
    	//javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    	cas[i][j].setPreferredSize(new Dimension(20,20));
    	cas[i][j].setLayout(new BorderLayout());
    	if(tablero.getCelda(i, j)==1)
    		cas[i][j].setBackground(new Color(0,100,0));
    	if(tablero.getCelda(i, j)==2)
    		cas[i][j].setBackground(new Color(255,150,0));
    	cas[i][j].setBorder(BorderFactory.createRaisedBevelBorder());
//        layout.setHorizontalGroup(
//            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGap(0, 161, Short.MAX_VALUE)
//        );
//        layout.setVerticalGroup(
//            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGap(0, 193, Short.MAX_VALUE)
//        );
//		JLabel im = new JLabel();
//		im.setIcon(hormiga);
//		cas[i][j].add(im,BorderLayout.CENTER);
	}

//    public void pintar(int x, int y){
//        this.casillas[x][y].setFondo(tocado);
//        this.repaint();
//    }
    
//    private void cargarImagenes() {
//        this.comida = this.cargarFondo("agua.gif");
//        this.hormiga = this.cargarFondo("tocado.gif");
//    }
    
    protected static ImageIcon cargarFondo(String ruta) {
        java.net.URL localizacion = TableroGUI.class.getResource(ruta);
        if (localizacion != null) {
            return new ImageIcon(localizacion);
        } else {
            System.err.println("No se ha encontrado el archivo: " + ruta);
            return null;
        }
    }
    
//    public int[] getCoordenadas(CasillasGUI casilla) {
//        int [] coordenadas = new int[2];
//        for (int i=0; i < this.casillas.length; i++) {
//            for (int j=0; j < this.casillas.length; j++) {
//                if (this.casillas[i][j] == casilla) {
//                    coordenadas[0] = i;
//                    coordenadas[1] = j;
//                }
//            }
//        }
//        return coordenadas;
//    }
    
//    public CasillasGUI[][] getCasillas() {
//        return casillas;
//    }
    
//    public void setCasillas(CasillasGUI[][] casillas) {
//        this.casillas = casillas;
//    }
//    
//    public boolean isTipoTablero() {
//        return tipoTablero;
//    }    
//    public void setTipoTablero(boolean tipoTablero) {
//        this.tipoTablero = tipoTablero;
//    }
                              
    private void initComponents() {

        setLayout(null);

        setBackground(new java.awt.Color(0, 0, 0));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setPreferredSize(new java.awt.Dimension(351, 351));
    }
    
    public static void main(String args[]) {
        TableroGUI tableroGUI1 = new TableroGUI(new Tablero(), 32);
        JFrame vent = new JFrame();
        vent.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(vent.getContentPane());
       
        vent.getContentPane().setLayout(new BorderLayout());

        vent.getContentPane().add(tableroGUI1,BorderLayout.CENTER);
        vent. pack();
        vent.setVisible(true);
    }
                     
}
